package apps.softtech.fastmarket.listenner;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import apps.softtech.fastmarket.R;

public class ListenerTabHost  implements TabHost.OnTabChangeListener{
	TabHost mTabHost;
	ViewPager mViewPager;
	Context mContext;
	private boolean isAdd=false;
	private int positionTab;
	public ListenerTabHost(TabHost mtaHost,ViewPager mPager,Context mContext){
		this.mTabHost = mtaHost;
		this.mTabHost.setOnTabChangedListener(this);
		this.mViewPager = mPager;
		this.mContext = mContext;
	}
	/**
	 * add tab
	 */
	public void createTab(){
		TabHost.TabSpec tabSpec;
		View vi = null;
		ImageView ivIcon = null;
		TextView tvText = null;
		Drawable dTab;
		String sTab;
		// tab home
		dTab = mContext.getResources().getDrawable(R.drawable.home);
		sTab = mContext.getResources().getString(R.string.home);

		tabSpec = mTabHost.newTabSpec(
				mContext.getResources().getString(R.string.tab1)).setIndicator(
				addIconTab(vi, ivIcon, tvText, dTab, sTab));
		tabSpec.setContent(new TabFactory(mContext));
		mTabHost.addTab(tabSpec);
		// tab category
		dTab = mContext.getResources().getDrawable(R.drawable.category);
		sTab = mContext.getResources().getString(R.string.category);

		tabSpec = mTabHost.newTabSpec(
				mContext.getResources().getString(R.string.tab2)).setIndicator(
				addIconTab(vi, ivIcon, tvText, dTab, sTab));
		tabSpec.setContent(new TabFactory(mContext));
		mTabHost.addTab(tabSpec);
		// tab new
		dTab = mContext.getResources().getDrawable(R.drawable.camera);
		sTab = mContext.getResources().getString(R.string.new_);

		tabSpec = mTabHost.newTabSpec(
				mContext.getResources().getString(R.string.tab3)).setIndicator(
				addIconTab(vi, ivIcon, tvText, dTab, sTab));
		tabSpec.setContent(new TabFactory(mContext));
		mTabHost.addTab(tabSpec);
		// tab notification
		dTab = mContext.getResources().getDrawable(R.drawable.notification);
		sTab = mContext.getResources().getString(R.string.notification);

		tabSpec = mTabHost.newTabSpec(
				mContext.getResources().getString(R.string.tab5)).setIndicator(
				addIconTab(vi, ivIcon, tvText, dTab, sTab));
		tabSpec.setContent(new TabFactory(mContext));
		mTabHost.addTab(tabSpec);
		// finish
		isAdd = true;
	}
	/**
	 * set background for tab bar
	 * @param v
	 * @param iv
	 * @param tv
	 * @param dtab
	 * @param Stab
	 * @return
	 */
	private View addIconTab(View v, ImageView iv, TextView tv,Drawable dtab, String Stab){
		LayoutInflater inf =  (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = inf.inflate(R.layout.item_tabhost, null);
		iv = (ImageView) v.findViewById(R.id.icon);
		iv.setImageDrawable(dtab);
		tv = (TextView)v.findViewById(R.id.title);
		tv.setText(Stab);
		return v;
	}
	@Override
	public void onTabChanged(String arg0) {
		if(isAdd){
		positionTab = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(positionTab);
		setBackgroundTabSelect();
		}
	}
	/**
	 * change backgroud when click
	 */
	public void setBackgroundTabSelect(){
		if (isAdd) {
			Log.v("so tab", ""+ mTabHost.getTabWidget().getChildCount());
			for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
				// unSelect tab
				mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
			}
			// select tab
			mTabHost.getTabWidget().getChildAt(positionTab)
					.setBackgroundColor(Color.parseColor("#f3f3f3f3"));
		}
	}

}

